<template>
	<div class="address">
		<div v-if="addlist.length>0" v-for="i in addlist" class="mart">
			<view class="weui-cells weui-cells_after-title" @click="routeradd(i)">
				<view class="weui-cell">
					<view class="weui-cell__bd">{{i.name}}{{i.tel}}</view>
					<view class="weui-cell__bd">{{i.province+i.city+i.area+i.address}}</view>
				</view>

			</view>
			<div class="control">
				<div class="controlitem" @click="edit(i.id)"><i class="iconfont">&#xe62e;</i><span>编辑</span> </div>
				<div class="controlitem" @click="del(i.id)"><i class="iconfont">&#xe644;</i><span>删除</span></div>
			</div>
			
		</div>

		<navigator url="/pages/addaddr/main"><button class="weui-btn addaddress" type="primary">
		添加新地址
		</button></navigator>
		
		<view class="abnor" v-if="isshowempty">
			<view class="abnor__box">
				<image class="abnor__image" mode="widthFix" src="https://s10.mogucdn.com/p2/161213/upload_74hhee883cbf190e3di6cljk23679_514x260.png" style="height: 110.778px;">
				</image>
				<view class="abnor__text">您还没有地址哦！~</view>
				
			</view>
		</view>
		
	</div>

</template>

<script>
	import { requests } from "../../../static/js/network"
	export default {
		data() {
			return {
				addlist: [],
				isshowempty:false
			}
		},
		onLoad() {
			Object.assign(this.$data, this.$options.data())
			if(this.$root.$mp.query.type && this.$root.$mp.query.type=='sebuy'){
					wx.setStorage({
							key: "waitsele",
							data: 'sebuy'
					})
			}
			this.init()
		},

		methods: {
			routeradd(obj){
				this.$store.dispatch('seleaddr',obj)
				if(this.$root.$mp.query.type && this.$root.$mp.query.type=='sebuy'){
					wx.redirectTo({ //保留当前页面，跳转到应用内的某个页面
						url: "/pages/orderbuy/main?type=sadd"
					})
					wx.setStorage({
							key: "waitsele",
							data: 'no'
					})
				}else if(wx.getStorageSync('waitsele')=='sebuy'){
					wx.redirectTo({ //保留当前页面，跳转到应用内的某个页面
						url: "/pages/orderbuy/main?type=sadd"
					})
					wx.setStorage({
							key: "waitsele",
							data: 'no'
					})
				}
			},
			del(ids) {
				let self = this
				wx.showModal({
					title: '是否删除',
					content: '删除当前地址',
					confirmText: "确定",
					cancelText: "取消",
					success: function(res) {
						console.log(res);
						if(res.confirm) {
							requests({
								url: 'https://tp5.blver.cn/api/wechat/deleteSendAddress',
								method: 'POST',
								header: {
									'content-type': 'application/json',
									'content-type': 'application/x-www-form-urlencoded' // 默认值
								},
								data: {
									session3rd: wx.getStorageSync('userinfo').session3rd,
									id: ids
								},
								success: function(res) {
									Object.assign(self.$data, self.$options.data())
									self.init()
									console.log(res)
									//self.content=self.classlist[0].goods_content.replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
								}
							})
						} else {
							console.log('用户点击辅助操作')
						}
					}
				});

			},
			init() {
				let self = this
				requests({
					url: 'https://tp5.blver.cn/api/wechat/listSendAddress',
					method: 'POST',
					header: {
						'content-type': 'application/json',
						'content-type': 'application/x-www-form-urlencoded' // 默认值
					},
					data: {
						session3rd: wx.getStorageSync('userinfo').session3rd
					},
					success: function(res) {
						if(res.data.data.length==0){
							self.isshowempty=true
						}else{
							self.isshowempty=false
							self.addlist = res.data.data
						}
						
						console.log(res)
						//self.content=self.classlist[0].goods_content.replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/"\/static/g, '"https://tp5.blver.cn/static').replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
					}
				})
			},
			edit(ids) {
				wx.navigateTo({
					url: '/pages/addaddr/main?id=' + ids
				})
			}
		}
	}
</script>

<style scoped>
	.control {
		display: flex;
		justify-content: flex-end;
		padding-right: 50rpx;
		background: #fff;
	}
	
	.controlitem {
		display: inline-block;
		margin-right: 20rpx;
	}
	
	.controlitem span,
	.controlitem i {
		display: inline-block;
		font-size: 28rpx;
		color: #999999;
	}
	.weui-cell{display: block; font-size: 28rpx;}
	.address {
		position: relative;
		height: 100vh;
		background:#D9D9D9 ;
	}
	
	.addaddress {
		position: absolute;
		width: 100%;
		bottom: 0;
		z-index: 99;
	}
	.mart{margin-bottom:15rpx ;}
</style>